package com.yitong.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.yitong.utils.RetResult.ResultMsg;

/**
 * @ClassName: RspUtils
 * @Description: TODO
 * @author xaoyaoyao
 * @date 2017年7月23日 下午1:02:18
 */
public class RspUtils {

	/**
	 * @Title: makeErrorRsp
	 * @Description: 返回内部错误
	 * @param msg
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeErrorRsp(String msg) {
		return makeRsp(RetCode.SYSERROR, msg);
	}

	/**
	 * @Title: makeRsp
	 * @Description: TODO
	 * @param code
	 * @param msg
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeRsp(int code, String msg) {
		Map<String, Object> rsp = new HashMap<String, Object>();
		rsp.put(ConstantUtils.CODE, code);
		rsp.put(ConstantUtils.MSG, msg);
		return rsp;
	}

	/**
	 * @Title: makeRsp
	 * @Description: data参数非空则返回带有data属性
	 * @param code
	 * @param msg
	 * @param data
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeRsp(int code, String msg, Object data) {
		Map<String, Object> rsp = new HashMap<String, Object>();
		rsp.put(ConstantUtils.CODE, code);
		rsp.put(ConstantUtils.MSG, msg);
		if (null != data) {
			rsp.put(ConstantUtils.DATA, data);
		}
		return rsp;
	}

	/**
	 * @Title: makeOKRsp
	 * @Description: 成功返回数据并且返回data属性
	 * @param kvs
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeOKRsp(Map<String, Object> kvs) {
		Map<String, Object> rsp = new HashMap<String, Object>();
		rsp.put(ConstantUtils.CODE, RetCode.SUCESS);
		rsp.put(ConstantUtils.MSG, ConstantUtils.SUCCESS);
		rsp.put(ConstantUtils.DATA, kvs);
		return rsp;
	}

	/**
	 * @Title: makeOKRsp
	 * @Description: 成功返回数据并且返回key属性
	 * @param key
	 * @param value
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeOKRsp(String key, Object value) {
		Map<String, Object> rsp = new HashMap<String, Object>();
		rsp.put(ConstantUtils.CODE, RetCode.SUCESS);
		rsp.put(ConstantUtils.MSG, ConstantUtils.SUCCESS);
		rsp.put(key, value);
		return rsp;
	}

	/**
	 * @Title: makeRsp
	 * @Description: 返回数据带有data属性
	 * @param value
	 *            data数据
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeRsp(Object value) {
		return makeOKRsp(ConstantUtils.DATA, value);
	}

	/**
	 * @Title: makeOkRsp
	 * @Description: 成功响应
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeOkRsp() {
		Map<String, Object> rsp = new HashMap<String, Object>();
		rsp.put(ConstantUtils.CODE, RetCode.SUCESS);
		rsp.put(ConstantUtils.MSG, ConstantUtils.SUCCESS);
		return rsp;
	}

	/**
	 * @Title: makeRsp
	 * @Description: 返回数据带没有data属性
	 * @param retResult
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeRsp(RetResult retResult) {
		if (null == retResult) {
			return makeOkRsp();
		}
		Map<String, Object> rsp = new HashMap<String, Object>();
		rsp.put(ConstantUtils.CODE, retResult.getCode());
		String msg = null;
		Object obj = retResult.getObj();
		boolean flag = true;
		if (null != obj) {
			ResultMsg result = null;
			try {
				result = (ResultMsg) obj;
			} catch (Exception e) {
			}
			if (null != result) {
				flag = false;
				msg = retResult.getMsg();
				if (StringUtils.isBlank(msg)) {
					flag = true;
				}
			}
		}
		if (flag) {
			msg = retResult.getMsg();
		}
		rsp.put(ConstantUtils.MSG, msg);
		Map<String, Object> map = retResult.getData();
		if (null != map && !map.isEmpty()) {
			rsp.put(ConstantUtils.DATA, map);
		}
		return rsp;
	}

	/**
	 * @Title: makeRsp
	 * @Description: 返回数据带有data属性
	 * @param retResult
	 * @param value
	 *            data数据
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> makeRsp(RetResult retResult, Object value) {
		if (null == retResult) {
			return makeOkRsp();
		}
		Map<String, Object> rsp = makeRsp(retResult);
		if (null != value) {
			rsp.put(ConstantUtils.DATA, value);
		}
		return rsp;
	}
}